#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/in.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>   /** type  */
#include <net/ip.h>
#include <net/netevent.h>
#include <net/route.h>
#include <net/ip_fib.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/netlink.h>
#include <linux/inetdevice.h>
#include <linux/list.h>
#include <linux/rcupdate.h>
#include <linux/export.h>
#include <net/tcp.h>
#include <net/fib_rules.h>

#include <uapi/linux/rtnetlink.h>


struct myfib_alias {
	struct hlist_node	fa_list;
	u8			fa_type;
};

/**
 *  sudo  arp-scan -I eth0  --localnet
 *  sudo  route
 *  路由子系统的核心是转发信息库(Forwarding Information Base,FIB),即路由表
 *
 *  RT_TABLE_DEFAULT=253,
 RT_TABLE_MAIN=254,
 RT_TABLE_LOCAL=255,
 pr_info("9999%s \n",__FUNCTION);

 RTM_NEWRULE

 /home/h/tb/kernel/kernel-tinker_board_2-debian_10-2.0.0/net/core/fib_rules.c
 *
 * 	/home/h/tb/kernel/kernel-tinker_board_2-debian_10-2.0.0/net/ipv4/fib_rules.c


 fib_add_ifaddr

 notify_rule_change

 */

struct net_device *mynet_device;

typedef unsigned int t_key;

int myprobe(struct platform_device *pdev) {

//	u8 a = 192;
//	u8 b = 168;
//	u8 c = 31;
//	u8 d = 25;
//
//	int ip_addr = (d << 24) | (c << 16) | (b << 8) | a;
//	pr_info("a = %d\n", d << 24);
//	pr_info("a = %d\n", c << 16);
//	pr_info("a = %dn", b << 8);


//	int a = 0xa8c0;
	struct fib_table  *main_table = fib_get_table(&init_net,RT_TABLE_LOCAL);
	struct trie *t;

	struct fib_alias *last;

	struct key_vector *pn, *n = t->kv;
	unsigned long index = 0;



	t = (struct trie *)main_table->tb_data;

	n = get_child_rcu(n,index);


			hlist_for_each_entry(last, &t->leaf, fa_list) {
				if (new->fa_slen < last->fa_slen)
					break;
				if ((new->fa_slen == last->fa_slen) &&
				    (new->tb_id > last->tb_id))
					break;
				fa = last;
			}



	struct key_vector *n, *pn;
		struct fib_alias *fa;
		unsigned long index;
		t_key cindex;


		pr_info("tb_id: %d\n", main_table->tb_id);

		pn = t->kv;
		n = get_child_rcu(pn, cindex);



	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner =
THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
